package org.liang.factory.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;

public abstract class FileReaderUtil {

	public static String read(InputStream inputStream, String charSet)
			throws UnsupportedEncodingException, IOException {
		StringBuffer sb = new StringBuffer();
		int n = -1;
		byte[] b = new byte[512];
		while ((n = inputStream.read(b)) != -1) {
			String temp = new String(b, 0, n, charSet);
			sb.append(temp);
		}

		inputStream.close();
		return sb.toString();
	}

	public static String read(InputStream inputStream) throws UnsupportedEncodingException, IOException {
		return read(inputStream, "utf-8");
	}

	public static String readOriginal(InputStream inputStream) throws UnsupportedEncodingException, IOException {
		StringBuffer sb = new StringBuffer();
		Scanner sc = new Scanner(inputStream);

		while ((sc.hasNextLine())) {
			sb.append(sc.nextLine()).append("\r\n");
		}
		sc.close();
		return sb.toString();
	}

}
